﻿namespace Program
{
    class Program
    {
        public static async Task Main2(string[] args)
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken token = cts.Token;
            DoWork(token);
            await Task.Delay(10000);
            Console.WriteLine("正在请求取消");
            cts.Cancel();

            try
            {
                //await DoWork;
            }
            catch (OperationCanceledException) 
            {
                Console.WriteLine("Task was Cancelled.");
            }

            cts.Dispose();

            Console.ReadKey();
        }
        static async ValueTask DoWork(CancellationToken token) 
        {
            for (int i = 0; i < 100; i++) 
            {
                if (token.IsCancellationRequested)
                {
                    Console.WriteLine("Operation was cancelled");
                    token.ThrowIfCancellationRequested();
                }

                Console.WriteLine($"Working...{i+1}");
                await Task.Delay(1000).ConfigureAwait(false);
            }
            
        }
    }
}